<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="城市名称" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:weather:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:weather:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:weather:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:weather:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="weatherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="城市ID" align="center" prop="cityid" />
      <el-table-column label="日期" align="center" prop="date" />
      <el-table-column label="星期" align="center" prop="week" />
      <el-table-column label="城市名称" align="center" prop="city" />
<!--      <el-table-column label="城市英文名称" align="center" prop="cityEn" />-->
      <el-table-column label="国家名称" align="center" prop="country" />
<!--      <el-table-column label="国家英文名称" align="center" prop="countryEn" />-->
      <el-table-column label="天气状况" align="center" prop="wea" />
<!--      <el-table-column label="天气状况对应的图片" align="center" prop="weaImg" />-->
      <el-table-column label="温度" align="center" prop="tem" />
      <el-table-column label="最高温度" align="center" prop="tem1" />
      <el-table-column label="最低温度" align="center" prop="tem2" />
      <el-table-column label="风向" align="center" prop="win" />
      <el-table-column label="风速" align="center" prop="winSpeed" />
      <el-table-column label="风力" align="center" prop="winMeter" />
      <el-table-column label="湿度" align="center" prop="humidity" />
      <el-table-column label="能见度" align="center" prop="visibility" />
      <el-table-column label="气压" align="center" prop="pressure" />
      <el-table-column label="降雨量" align="center" prop="rainPcpn" />
      <el-table-column label="空气质量指数" align="center" prop="air" />
      <el-table-column label="PM2.5浓度" align="center" prop="airPm25" />
      <el-table-column label="空气质量级别" align="center" prop="airLevel" />
      <el-table-column label="空气质量提示" align="center" prop="airTips" />
<!--      <el-table-column label="天气预警信息" align="center" prop="alarm" />-->
      <el-table-column label="紫外线指数" align="center" prop="uvIndex" />
      <el-table-column label="紫外线描述" align="center" prop="uvDescription" />
      <el-table-column label="白天天气状况" align="center" prop="weaDay" />
<!--      <el-table-column label="白天天气对应的图片" align="center" prop="weaDayImg" />-->
      <el-table-column label="夜晚天气状况" align="center" prop="weaNight" />
<!--      <el-table-column label="夜晚天气对应的图片" align="center" prop="weaNightImg" />-->
      <el-table-column label="日出时间" align="center" prop="sunrise" />
      <el-table-column label="日落时间" align="center" prop="sunset" />
<!--      <el-table-column label="${comment}" align="center" prop="nums" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:weather:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:weather:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改城市天气对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="城市ID" prop="cityid">
          <el-input v-model="form.cityid" placeholder="请输入城市ID" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-input v-model="form.date" placeholder="请输入日期" />
        </el-form-item>
        <el-form-item label="星期" prop="week">
          <el-input v-model="form.week" placeholder="请输入星期" />
        </el-form-item>
        <el-form-item label="城市名称" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市名称" />
        </el-form-item>
        <el-form-item label="城市英文名称" prop="cityEn">
          <el-input v-model="form.cityEn" placeholder="请输入城市英文名称" />
        </el-form-item>
        <el-form-item label="国家名称" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家名称" />
        </el-form-item>
        <el-form-item label="国家英文名称" prop="countryEn">
          <el-input v-model="form.countryEn" placeholder="请输入国家英文名称" />
        </el-form-item>
        <el-form-item label="天气状况" prop="wea">
          <el-input v-model="form.wea" placeholder="请输入天气状况" />
        </el-form-item>
        <el-form-item label="天气状况对应的图片" prop="weaImg">
          <el-input v-model="form.weaImg" placeholder="请输入天气状况对应的图片" />
        </el-form-item>
        <el-form-item label="温度" prop="tem">
          <el-input v-model="form.tem" placeholder="请输入温度" />
        </el-form-item>
        <el-form-item label="最高温度" prop="tem1">
          <el-input v-model="form.tem1" placeholder="请输入最高温度" />
        </el-form-item>
        <el-form-item label="最低温度" prop="tem2">
          <el-input v-model="form.tem2" placeholder="请输入最低温度" />
        </el-form-item>
        <el-form-item label="风向" prop="win">
          <el-input v-model="form.win" placeholder="请输入风向" />
        </el-form-item>
        <el-form-item label="风速" prop="winSpeed">
          <el-input v-model="form.winSpeed" placeholder="请输入风速" />
        </el-form-item>
        <el-form-item label="风力" prop="winMeter">
          <el-input v-model="form.winMeter" placeholder="请输入风力" />
        </el-form-item>
        <el-form-item label="湿度" prop="humidity">
          <el-input v-model="form.humidity" placeholder="请输入湿度" />
        </el-form-item>
        <el-form-item label="能见度" prop="visibility">
          <el-input v-model="form.visibility" placeholder="请输入能见度" />
        </el-form-item>
        <el-form-item label="气压" prop="pressure">
          <el-input v-model="form.pressure" placeholder="请输入气压" />
        </el-form-item>
        <el-form-item label="降雨量" prop="rainPcpn">
          <el-input v-model="form.rainPcpn" placeholder="请输入降雨量" />
        </el-form-item>
        <el-form-item label="空气质量指数" prop="air">
          <el-input v-model="form.air" placeholder="请输入空气质量指数" />
        </el-form-item>
        <el-form-item label="PM2.5浓度" prop="airPm25">
          <el-input v-model="form.airPm25" placeholder="请输入PM2.5浓度" />
        </el-form-item>
        <el-form-item label="空气质量级别" prop="airLevel">
          <el-input v-model="form.airLevel" placeholder="请输入空气质量级别" />
        </el-form-item>
        <el-form-item label="空气质量提示" prop="airTips">
          <el-input v-model="form.airTips" placeholder="请输入空气质量提示" />
        </el-form-item>
        <el-form-item label="紫外线指数" prop="uvIndex">
          <el-input v-model="form.uvIndex" placeholder="请输入紫外线指数" />
        </el-form-item>
        <el-form-item label="紫外线描述" prop="uvDescription">
          <el-input v-model="form.uvDescription" placeholder="请输入紫外线描述" />
        </el-form-item>
        <el-form-item label="白天天气状况" prop="weaDay">
          <el-input v-model="form.weaDay" placeholder="请输入白天天气状况" />
        </el-form-item>
        <el-form-item label="白天天气对应的图片" prop="weaDayImg">
          <el-input v-model="form.weaDayImg" placeholder="请输入白天天气对应的图片" />
        </el-form-item>
        <el-form-item label="夜晚天气状况" prop="weaNight">
          <el-input v-model="form.weaNight" placeholder="请输入夜晚天气状况" />
        </el-form-item>
        <el-form-item label="夜晚天气对应的图片" prop="weaNightImg">
          <el-input v-model="form.weaNightImg" placeholder="请输入夜晚天气对应的图片" />
        </el-form-item>
        <el-form-item label="日出时间" prop="sunrise">
          <el-input v-model="form.sunrise" placeholder="请输入日出时间" />
        </el-form-item>
        <el-form-item label="日落时间" prop="sunset">
          <el-input v-model="form.sunset" placeholder="请输入日落时间" />
        </el-form-item>
        <el-form-item label="${comment}" prop="nums">
          <el-input v-model="form.nums" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWeather, getWeather, delWeather, addWeather, updateWeather } from "@/api/business/weather";

export default {
  name: "Weather",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 城市天气表格数据
      weatherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cityid: null,
        date: null,
        week: null,
        city: null,
        cityEn: null,
        country: null,
        countryEn: null,
        wea: null,
        weaImg: null,
        tem: null,
        tem1: null,
        tem2: null,
        win: null,
        winSpeed: null,
        winMeter: null,
        humidity: null,
        visibility: null,
        pressure: null,
        rainPcpn: null,
        air: null,
        airPm25: null,
        airLevel: null,
        airTips: null,
        alarm: null,
        uvIndex: null,
        uvDescription: null,
        weaDay: null,
        weaDayImg: null,
        weaNight: null,
        weaNightImg: null,
        sunrise: null,
        sunset: null,
        nums: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询城市天气列表 */
    getList() {
      this.loading = true;
      listWeather(this.queryParams).then(response => {
        this.weatherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cityid: null,
        date: null,
        week: null,
        updateTime: null,
        city: null,
        cityEn: null,
        country: null,
        countryEn: null,
        wea: null,
        weaImg: null,
        tem: null,
        tem1: null,
        tem2: null,
        win: null,
        winSpeed: null,
        winMeter: null,
        humidity: null,
        visibility: null,
        pressure: null,
        rainPcpn: null,
        air: null,
        airPm25: null,
        airLevel: null,
        airTips: null,
        alarm: null,
        uvIndex: null,
        uvDescription: null,
        weaDay: null,
        weaDayImg: null,
        weaNight: null,
        weaNightImg: null,
        sunrise: null,
        sunset: null,
        nums: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加城市天气";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWeather(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改城市天气";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWeather(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWeather(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除城市天气编号为"' + ids + '"的数据项？').then(function() {
        return delWeather(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/weather/export', {
        ...this.queryParams
      }, `weather_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
